package com.smartHire.controller.User.Cart;

import com.smartHire.domain.Cart;
import com.smartHire.service.CartService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AddCartServlet", value = "/AddCartServlet")
public class AddCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String open_id= request.getParameter("open_id");
        String goods_id = request.getParameter("goods_id");
        String time = request.getParameter("time");
        String num = request.getParameter("num");
        System.out.println("Aaaaaaaaaaaaaa");
        System.out.println(time);
        String price = request.getParameter("price");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            CartService cartService = (CartService) ctx.getBean("cartService");

            Cart cart = new Cart();
            cart.setU_Id(open_id);
            cart.setGoods(Integer.parseInt(goods_id));
            cart.setMoney(Float.parseFloat(price));
            cart.setNumber(Integer.parseInt(num));
            cart.setDay(Integer.parseInt(time));

//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = sdf.parse(time);
//            Date now = new Date();
//            System.out.println(date);
//            System.out.println(date.getDate());
//            System.out.println(now);
//            System.out.println(now.getDate());
//            int day = date.getDate() - now.getDate();

//            cart.setDay(day);

            System.out.println(cart);

            cartService.addCart(cart);

        }catch (Exception e)
        {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
